Skip to content

test: cover linalg, random, factory, dtype, parameter types and ArrayAt - #42

Merged
Gajesh2007 merged 7 commits into
mainfrom
test/linalg-random-factory
Oct 2, 2026
Merged

Gajesh2007 merged 7 commits into
mainfrom
test/linalg-random-factory

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed changes

This PR adds tests only. It does not change library code.

Coverage on the base (run 36587565184) was low for six files in Source/MLX: Linalg.swift 31.5%, Random.swift 53.1%, Factory.swift 43.2%, DType.swift 50.8%, ParameterTypes.swift 14.6%, ArrayAt.swift 25.0%. This PR adds six new test files in Tests/MLXTests:

  • LinalgCoverageTests (22 tests): each norm overload against hand values (vector orders 0, 1, 2, 3, inf, -inf; matrix orders fro, nuc, 1, -1, 2, -2, inf, -inf; axis, axes, IntOrArray and keepDims forms; the error for fro/nuc with one axis). The factorizations rebuild the input: QR (Q*R == A, Q orthonormal, R upper triangular), SVD (U*diag(S)*Vt == A), Cholesky (L*L^T == A), inv (A*inv == I), triInv, choleskyInv, pinv, lu (L[P]*U == A), lu_factor, solve, solveTriangular, eig, eigvals, eigh and eigvalsh (A*v == v*w, and UPLO reads only one triangle), and cross. Both the MLXLinalg functions and the free functions are called.
  • RandomCoverageTests (20 tests): each sampler, as MLXRandom function and free function: seed, key, split, uniform, normal, multivariateNormal, permutation, randInt, bernoulli, truncatedNormal, gumbel, categorical, laplace. Each test checks seeded determinism, shape, dtype and range, and the mean and variance of 4000 samples with a wide tolerance.
  • FactoryCoverageTests (10 tests): each MLXArray factory and free factory (zeros, ones, eye, identity, full, linspace, arange, repeated, tri) with exact values and dtypes.
  • DTypeCoverageTests (17 tests): the DType properties for all 14 cases, finfo, Codable, each asMLXArray conversion, toArrays, and the MLX promotion table (mlx/dtype.cpp, promote_types) through binary operations.
  • ParameterTypesCoverageTests (4 tests): each init and literal form of IntOrPair, IntOrTriple, IntOrArray and FloatOrArray.
  • ArrayAtCoverageTests (10 tests): each .at[...] update (add, subtract, multiply, divide, minimum, maximum) against a Swift loop, with repeated indices; index, slice, strided slice, two-index and full-slice forms; the sequence subscript and the stream: label.

The linalg factorizations run only on the CPU in MLX, so those tests pass stream: .cpu.

Defects found

Three tests find library defects. Each test keeps the assertion and wraps only the failing assertions in XCTExpectFailure:

The fix PRs merge before this PR. Before this PR merges, remove the wrappers (see the checklist below).

fork.yaml: the Tests section text now names the new test files.

Before this PR merges: remove the wrappers

The fix PRs (#43 and #44) merge first. The wrappers stay on this branch until then, because the fixes are not on its base. After the fixes have merged, merge main into this branch (no rebase), do the steps below, wait for Fork Tests, and then merge this PR. XCTest reports an unexpected pass inside XCTExpectFailure as a failure, so this PR must not merge with the wrappers after the fixes are on main. Line numbers are at head f97466d. In each step, remove the XCTExpectFailure(...) { line and its closing }, keep the assertions inside, and delete the "Defect" comment above it.

Base and merge order. This PR is based on ci/coverage-scope (#35). It merges after #35. It does not depend on #36-#38 or on the sibling coverage PRs (#41 and others). If a sibling test PR merges first, the only overlap is the text at the end of the Tests section in fork.yaml. Keep both lines.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes (ran swift format format --in-place --configuration .swift-format on the changed files)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed) (fork.yaml; check_forkdiff.py passes)

🤖 Generated with Claude Code

davidtai and others added 3 commits September 29, 2026 09:06
The job summary now shows one table with Lines, Regions and Functions for
all of Source/MLX* and for MLX, MLXNN and MLXOptimizers, each against the
80% target. The target is reported only. The text under the table names
what is not measured: the Cmlx core and the targets that the test bundle
does not link.

The step reads each value from the TOTAL row by its column name and fails
when a value is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MLXFast, MLXFFT, MLXLinalg and MLXRandom are deprecated shims that forward
to APIs in Source/MLX. Those APIs are counted in the Source/MLX row, so the
text no longer reads as if FFT, linalg and random were unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add six test files for code in Source/MLX that had low line coverage:

- LinalgCoverageTests: each norm overload with hand values, and qr, svd,
  inv, triInv, cholesky, choleskyInv, pinv, cross, lu, lu_factor, solve,
  solveTriangular, eig, eigvals, eigh and eigvalsh. The tests rebuild the
  input from the factors or compare with values worked out by hand.
- RandomCoverageTests: each sampler and its free function, with seeded
  determinism, shape, dtype, range and moment checks.
- FactoryCoverageTests: each MLXArray factory and free factory, with exact
  values and dtypes.
- DTypeCoverageTests: DType properties, finfo, Codable, each asMLXArray
  conversion, toArrays and the MLX promotion table.
- ParameterTypesCoverageTests: each init and literal form of IntOrPair,
  IntOrTriple, IntOrArray and FloatOrArray.
- ArrayAtCoverageTests: each .at[...] update compared with a Swift loop,
  with repeated indices.

Three tests find defects. They wrap only the failing assertions in
XCTExpectFailure:

- Linalg.swift:717: the free triInv does not pass `upper` on.
- DType.swift:204: bfloat16 smallestSubnormal is the smallest normal.
- DType.swift:137-205: complex64 finfo uses float64 limits, not float32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
davidtai and others added 3 commits September 30, 2026 21:45
MLX does not allow float64 on the GPU, so the float64 cases stopped the
test process. Make the arrays and the add on the CPU stream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Inside an XCTestCase, add(...) refers to the XCTestCase method.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The GPU computes linspace in float32, so some values differ from whole
numbers by one or two ulp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Gajesh2007 Gajesh2007 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed test-only diff at head 7252fbf and additive fork.yaml resolution. Normal signed main integration preserves all previously landed tests and CI. Runtime Source is unchanged; original dtype/linalg test blobs and all three known-defect XCTExpectFailure markers are preserved while #43/#44 remain unmerged. User authorizes merging with new CI pending.

@Gajesh2007
Gajesh2007 merged commit 20a8b47 into main Oct 2, 2026
7 checks passed
@davidtai
davidtai deleted the test/linalg-random-factory branch October 4, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants